package cn.b.sky.mes.hr.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import cn.b.sky.mes.hr.dao.PersonnelTypeDao;
import cn.b.sky.mes.hr.model.PersonnelType;
import cn.b.sky.mes.hr.model.condition.PersonnelTypeCondition;
import cn.b.sky.mes.hr.service.PersonnelTypeService;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.utils.DatabaseUtils;
import cn.b.sky.i18n.utils.LocaleResourceBundle;
import cn.b.sky.user.SecurityContextUtil;

@Service
public class PersonnelTypeServiceImpl implements PersonnelTypeService {

  private static final Logger LOG = LoggerFactory.getLogger(PersonnelTypeServiceImpl.class);

  @Autowired
  private PersonnelTypeDao personnelTypeDao;

  @Override
  public List<PersonnelType> getPersonnelTypeListByCondition(PersonnelTypeCondition condition) {
    condition.setSiteCode(SecurityContextUtil.getSiteCode());
    return personnelTypeDao.getPersonnelTypeListByCondition(condition);
  }

  @Override
  public int getPersonnelTypeCountByCondition(PersonnelTypeCondition condition) {
    condition.setSiteCode(SecurityContextUtil.getSiteCode());
    return personnelTypeDao.getPersonnelTypeCountByCondition(condition);
  }

  @Override
  public BatchJobReport deletePersonnelType(String siteCode, String personnelTypeCode) {
    String cause = "";
    Boolean success = true;
    try {
      PersonnelType personnelType = new PersonnelType();
      personnelType.setSiteCode(siteCode);
      personnelType.setPersonnelTypeCode(personnelTypeCode);
      personnelTypeDao.deletePersonnelType(personnelType);
    } catch (Exception e) {
      LOG.error("{}", e);
      TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
      if ((DatabaseUtils.isConstraintsError(e.getMessage()))) {
        // 外键删除已存在提示
        cause = LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),SecurityContextUtil.getLanguageCode(),
            "dataBaseOperationPkFail.M")
            + LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),SecurityContextUtil.getLanguageCode(), "notDelete.M");
      } else {
        cause = e.getMessage();
      }
      success = false;
    }
    return new BatchJobReport(personnelTypeCode, cause, success, SecurityContextUtil.getLanguageCode(),SecurityContextUtil.getSiteCode());
  }

  @Override
  public void insertPersonnelType(PersonnelType personnelType) {
    if (personnelType != null && !StringUtils.isBlank(personnelType.getPersonnelTypeCode())) {
      personnelTypeDao.insertPersonnelType(personnelType);
    }
  }

  @Override
  public void updatePersonnelType(PersonnelType personnelType) {
    personnelTypeDao.updatePersonnelType(personnelType);
  }

  @Override
  public PersonnelType getPersonnelTypeInfo(String siteCode, String personnelTypeCode) {
    return personnelTypeDao.getPersonnelTypeByPk(siteCode, personnelTypeCode);
  }

  @Override
  public List<PersonnelType> getPersonnelTypeSuggestByCondition(PersonnelTypeCondition condition) {
    if (condition == null) {
      return null;
    }
    if (StringUtils.isBlank(condition.getOrderField())) {
      condition.setOrderField("createDateTime");//人员编号排序
    }
    return personnelTypeDao.getPersonnelTypeSuggestByCondition(condition);
  }

}
